package kr.hkit.androidcomplete01.ch08;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class FreeLine extends Activity {
	private MyView mv;
	
	public class Vertex{
		Vertex(float ax, float ay, boolean ad){
			x = ax;
			y = ay;
			isDraw = ad;
		}
		float x;
		float y;
		boolean isDraw;
	}
	
	ArrayList<Vertex> arVertex;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mv = new MyView(this);
		setContentView(mv);
		
		arVertex = new ArrayList<Vertex>();
	}

	protected class MyView extends View{
		Paint mPaint;
		
		public MyView(Context context) {
			super(context);
			
			mPaint = new Paint();
			mPaint.setColor(Color.BLACK);
			mPaint.setStrokeWidth(3);
			mPaint.setAntiAlias(true);
		}
		
		public void onDraw(Canvas canvas){
			canvas.drawColor(Color.LTGRAY);
			
			for(int i=0;i<arVertex.size();i++){
				if(arVertex.get(i).isDraw){
					canvas.drawLine(arVertex.get(i-1).x, arVertex.get(i-1).y, arVertex.get(i).x, arVertex.get(i).y, mPaint);
				}
			}
		}
		
		public boolean onTouchEvent(MotionEvent event){
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				arVertex.add(new Vertex(event.getX(), event.getY(), false));
				return true;
			}
			if(event.getAction() == MotionEvent.ACTION_MOVE){
				arVertex.add(new Vertex(event.getX(), event.getY(), true));
				return true;
			}
			return false;
		}
	}
}
